package com.aaa.controller.system;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.aaa.controller.BaseController;
import com.aaa.entity.system.PageOrder;
import com.aaa.entity.Result;
import com.aaa.entity.system.User;
import com.aaa.service.system.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author ：肖成杰（1752167034@qq.com）
 * @date ：Created in 2022/8/9 14:10
 * @description：
 * @modified By：
 * @version:
 */
@Controller
@RequestMapping("user")
public class UserServlet extends BaseController {

    @Autowired
    UserService userService;

    @RequestMapping("query.do")
    @ResponseBody
    public String queryAll(PageOrder pageOrder, User user){
        Page page = userService.queryAll(pageOrder, user);

        JSONConfig jsonConfig = JSONConfig.create();
        jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        String jsonStr = JSONUtil.toJsonStr(success(page.getTotal(),page.getResult()), jsonConfig);
        return jsonStr;
    }

    @RequestMapping("delete.do")
    @ResponseBody
    public Result delete(int[] userid){

        boolean b = userService.delete(userid);
        if(b){
            return  success("删除成功");
        }
        return fail("删除失败");
    }


    @RequestMapping("insert.do")
    @ResponseBody
    public Result insertUser(User user){
        boolean insert = userService.insertUser(user);
        if(insert){
           return success();
        }else {
           return fail();
        }
    }

    @RequestMapping("update.do")
    @ResponseBody
    public Result updateUser(User user){
        boolean updateUser = userService.updateUser(user);
        if(updateUser){
            return success();
        }else {
            return fail();
        }
    }

    @RequestMapping("updatePassword.do")
    @ResponseBody
    public Result updatePassword(User user){
        boolean updateUser = userService.upadtePassword(user);
        if(updateUser){
            return success();
        }else {
            return fail();
        }
    }
    @RequestMapping("resetPassword.do")
    @ResponseBody
    public Result resetPassword(User user){
        boolean updateUser = userService.resetPassword(user);
        if(updateUser){
            return success();
        }else {
            return fail();
        }
    }

    @RequestMapping("queryUserByName.do")
    @ResponseBody
    public Result queryUserByName(User user){
        User newUser = userService.queryUserByName(user);
        if(ObjectUtil.isNotEmpty(newUser)){
            return success("您起的名字太受欢迎了！请换一个吧！！！");
        }else {
            return fail();
        }
    }

    @RequestMapping("loginAjax.do")
    @ResponseBody
    public Result loginAjax(User user){
        boolean queryName = userService.queryName(user);
        if(queryName){
            return success();
        }else {
            return fail();
        }
    }

    //用户查询页面
    @RequestMapping("toShowUser.do")
    public ModelAndView toShowUser(ModelAndView modelAndView){
        modelAndView.setViewName("system/showUser");
        return modelAndView;
    }

}